{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:16:28.878333Z",
     "start_time": "2025-09-30T02:16:26.923055Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "df = pd.read_csv('univ.csv', encoding='gbk')\n",
    "df"
   ],
   "id": "f3fc81a42d6a228e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "     名次    学校名称      总分  类型 所在省份 所在城市     办学方向 主管部门\n",
       "0     1    北京大学  100.00  综合   北京  北京市    中国研究型  教育部\n",
       "1     2    清华大学   98.50  理工   北京  北京市    中国研究型  教育部\n",
       "2     3    复旦大学   82.79  综合   上海  上海市    中国研究型  教育部\n",
       "3     4    武汉大学   82.43  综合   湖北  武汉市    中国研究型  教育部\n",
       "4     5    浙江大学   82.38  综合   浙江  杭州市    中国研究型  教育部\n",
       "..  ...     ...     ...  ..  ...  ...      ...  ...\n",
       "95   96  浙江师范大学   63.37  师范   浙江  金华市  区域特色研究型  浙江省\n",
       "96   97    安徽大学   63.34  综合   安徽  合肥市    区域研究型  安徽省\n",
       "97   98  首都医科大学   63.32  医药   北京  北京市  区域特色研究型  北京市\n",
       "98   99    江南大学   63.31  综合   江苏  无锡市  区域特色研究型  教育部\n",
       "99  100    山西大学   63.29  综合   山西  太原市    区域研究型  山西省\n",
       "\n",
       "[100 rows x 8 columns]"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>名次</th>\n",
       "      <th>学校名称</th>\n",
       "      <th>总分</th>\n",
       "      <th>类型</th>\n",
       "      <th>所在省份</th>\n",
       "      <th>所在城市</th>\n",
       "      <th>办学方向</th>\n",
       "      <th>主管部门</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>北京大学</td>\n",
       "      <td>100.00</td>\n",
       "      <td>综合</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>清华大学</td>\n",
       "      <td>98.50</td>\n",
       "      <td>理工</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>复旦大学</td>\n",
       "      <td>82.79</td>\n",
       "      <td>综合</td>\n",
       "      <td>上海</td>\n",
       "      <td>上海市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>武汉大学</td>\n",
       "      <td>82.43</td>\n",
       "      <td>综合</td>\n",
       "      <td>湖北</td>\n",
       "      <td>武汉市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>浙江大学</td>\n",
       "      <td>82.38</td>\n",
       "      <td>综合</td>\n",
       "      <td>浙江</td>\n",
       "      <td>杭州市</td>\n",
       "      <td>中国研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>96</td>\n",
       "      <td>浙江师范大学</td>\n",
       "      <td>63.37</td>\n",
       "      <td>师范</td>\n",
       "      <td>浙江</td>\n",
       "      <td>金华市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>浙江省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>97</td>\n",
       "      <td>安徽大学</td>\n",
       "      <td>63.34</td>\n",
       "      <td>综合</td>\n",
       "      <td>安徽</td>\n",
       "      <td>合肥市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>安徽省</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>首都医科大学</td>\n",
       "      <td>63.32</td>\n",
       "      <td>医药</td>\n",
       "      <td>北京</td>\n",
       "      <td>北京市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>北京市</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>99</td>\n",
       "      <td>江南大学</td>\n",
       "      <td>63.31</td>\n",
       "      <td>综合</td>\n",
       "      <td>江苏</td>\n",
       "      <td>无锡市</td>\n",
       "      <td>区域特色研究型</td>\n",
       "      <td>教育部</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>山西大学</td>\n",
       "      <td>63.29</td>\n",
       "      <td>综合</td>\n",
       "      <td>山西</td>\n",
       "      <td>太原市</td>\n",
       "      <td>区域研究型</td>\n",
       "      <td>山西省</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 8 columns</p>\n",
       "</div>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "collapsed": true
   },
   "cell_type": "markdown",
   "source": [
    "# 四、数据类型\n",
    "\n",
    "## 4.1 Pandas支持的数据类型\n",
    "\n",
    "具体类型是Python，Numpy各种类型的混合，可以比下表分的更细，如float16，float32，float64等。常用数据类型如下：\n",
    "\n",
    "- 数值：\n",
    "    - float\n",
    "    - int\n",
    "    - 逻辑：bool\n",
    "    - 日期：datetime64[ns], datetime64[ns, tz], timedelta[ns]\n",
    "- 非数值：\n",
    "    - string：实际上也是object，不推荐使用string\n",
    "    - category：实际上是以数值编码+标签形式存储；可存储次序关系（有序分类）\n",
    "    - object：凡是无法看作是数值的数据统称为object类型"
   ],
   "id": "5a6792ab422633bb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:51:29.020966Z",
     "start_time": "2025-09-29T14:51:29.016594Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看各列的数据类型\n",
    "df.dtypes"
   ],
   "id": "3c21e754325b1fe2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "名次        int64\n",
       "学校名称     object\n",
       "总分      float64\n",
       "类型       object\n",
       "所在省份     object\n",
       "所在城市     object\n",
       "办学方向     object\n",
       "主管部门     object\n",
       "dtype: object"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### 4.2 Category分类对象",
   "id": "14f0343ed22e8ca8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:54:39.453393Z",
     "start_time": "2025-09-29T14:54:39.449839Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 创建一个有序的分类\n",
    "cate1 = pd.Categorical(\n",
    "    ['a', 'b', 'c', 'a', 'b', 'c'],\n",
    "    ordered=True,\n",
    "    # 规定c<b<a\n",
    "    categories=['c', 'b', 'a'])\n",
    "print(type(cate1))\n",
    "print(cate1)"
   ],
   "id": "a354a291390884ce",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.arrays.categorical.Categorical'>\n",
      "['a', 'b', 'c', 'a', 'b', 'c']\n",
      "Categories (3, object): ['c' < 'b' < 'a']\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "如果数据框中需要用到Category类型，没有必要手动创建Categirial，可以使用以下三种方式：\n",
    "\n",
    "- 直接指定：在pd.Series()和pd.DataFrame中使用dtype=\"category\"选项。\n",
    "- 类型转换：df.astype()。\n",
    "- 函数生成：如 pd.cut()"
   ],
   "id": "db4f62b2478f790f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:57:03.297538Z",
     "start_time": "2025-09-29T14:57:03.292839Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将某列指定为category\n",
    "pd.Series([\"a\", \"b\", \"c\", \"a\"], dtype=\"category\")"
   ],
   "id": "77fba43de818e9ff",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "2    c\n",
       "3    a\n",
       "dtype: category\n",
       "Categories (3, object): ['a', 'b', 'c']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-29T14:58:18.802701Z",
     "start_time": "2025-09-29T14:58:18.795318Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 在df中指定时必须各列一起指定为相同类型\n",
    "df1 = pd.DataFrame(data = [[1,\"test\"], [2,\"train\"],\n",
    "                           [3,\"test\"],[4,\"train\"]],\n",
    "                   columns = [ 'var2', 'var3' ],\n",
    "                   dtype=\"category\" # 所有列都被指定为category\n",
    "                  )\n",
    "print(df1.dtypes)\n",
    "df1"
   ],
   "id": "d9f56d60ab9e0340",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "var2    category\n",
      "var3    category\n",
      "dtype: object\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "  var2   var3\n",
       "0    1   test\n",
       "1    2  train\n",
       "2    3   test\n",
       "3    4  train"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>var2</th>\n",
       "      <th>var3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>test</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>test</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>train</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 3.2 类型转换\n",
    "\n",
    "语法：\n",
    "\n",
    "``` python\n",
    "df.astype(\n",
    "    dtype, #  指定希望转换的数据类型，可以使用numpy或者python中的数据类型: int/float/bool/str\n",
    "    copy = True, #  是否生成新的副本，而不是替换原数据框\n",
    "    errors = 'raise', #  转换出错时是否抛出错误，'raise'/'ignore'\n",
    ")\n",
    "```"
   ],
   "id": "fa3d2e4457506136"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:17:33.526840Z",
     "start_time": "2025-09-30T02:17:33.522697Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# print(df.head(3))\n",
    "df.dtypes"
   ],
   "id": "42181c170b4412c7",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "名次        int64\n",
       "学校名称     object\n",
       "总分      float64\n",
       "类型       object\n",
       "所在省份     object\n",
       "所在城市     object\n",
       "办学方向     object\n",
       "主管部门     object\n",
       "dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:19:18.481759Z",
     "start_time": "2025-09-30T02:19:18.477768Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将所有列转换为object\n",
    "df1 = df.astype('object')\n",
    "df1.dtypes"
   ],
   "id": "c66c859386506a44",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "名次      object\n",
       "学校名称    object\n",
       "总分      object\n",
       "类型      object\n",
       "所在省份    object\n",
       "所在城市    object\n",
       "办学方向    object\n",
       "主管部门    object\n",
       "dtype: object"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:23:27.260126Z",
     "start_time": "2025-09-30T02:23:27.254383Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 只修改某列的数据类型\n",
    "# 这种写法会截取某列得到一个新的序列\n",
    "df2 = df.名次.astype('int16')\n",
    "df2.head(5)\n",
    "type(df2)"
   ],
   "id": "a1254ac4072002fa",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "pandas.core.series.Series"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "明确指定转换类型的函数：\n",
    "\n",
    "- pd.to_datetime()\n",
    "- pd.to_timedelta()\n",
    "- pd.to_numeric()\n",
    "- df.to_string()\n",
    "\n",
    "可以配合df.apply来批量进行多列的转换\n",
    "\n",
    "df.infer_objects() 基于数据特征进行自动转换数据类型"
   ],
   "id": "dfd07f8dc95df3ed"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:26:43.612093Z",
     "start_time": "2025-09-30T02:26:43.608785Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将某列转换为数值类型\n",
    "pd.to_numeric(df.总分)\n",
    "df.dtypes"
   ],
   "id": "b51f0d18e59eef2e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "名次        int64\n",
       "学校名称     object\n",
       "总分      float64\n",
       "类型       object\n",
       "所在省份     object\n",
       "所在城市     object\n",
       "办学方向     object\n",
       "主管部门     object\n",
       "dtype: object"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:34:03.548610Z",
     "start_time": "2025-09-30T02:34:03.543638Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 先将名次和总分列转换为object类型\n",
    "# df[['名次', '总分']].astype('object').dtypes\n",
    "# 在使用apply转换为数值类型\n",
    "df[['名次', '总分']].astype('object').apply(pd.to_numeric).dtypes"
   ],
   "id": "2458e38aebcf956d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "名次      int64\n",
       "总分    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-30T02:38:38.964862Z",
     "start_time": "2025-09-30T02:38:38.959414Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 自动转换数据类型\n",
    "df = pd.DataFrame({\"A\": [\"a\", 1, 2, 3]})\n",
    "df = df.iloc[1:]\n",
    "print(df)\n",
    "print('---')\n",
    "print(df.dtypes)\n",
    "print('---')\n",
    "df.infer_objects().dtypes"
   ],
   "id": "3fed0aabb801a5ad",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   A\n",
      "1  1\n",
      "2  2\n",
      "3  3\n",
      "---\n",
      "A    object\n",
      "dtype: object\n",
      "---\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "A    int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 56
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
